<template>
	<view class="production-analysis-page">
		<!-- 顶部导航栏 -->
		<u-navbar :placeholder="true" leftIcon=""></u-navbar>
		<image class="head-bg" src="/static/images/headBg.jpg" alt=""></image>
		<view class="page-content">
			<view class="header">
				<view class="header-left">
					<text>生产分析</text>
					<text>数据更新: {{getCurrentDate()}}</text>
				</view>
				<view class="header-right">
					<view class="date-selector" @click="selectDate">
						<text class="date-text">{{showTime}}</text>
						<u-icon name="arrow-down-fill" size="10" color="#666"></u-icon>
					</view>
				</view>
			</view>
			<!-- KPI指标卡片 -->
			<view class="kpi-section">
				<view class="kpi-cards">
					<view>
						<view v-for="(kpi, index) in kpiData" :key="index" class="kpi-card">
							<view class="kpi-value">
								<text>{{ kpi.value }}</text>
								<text>万吨</text>
							</view>
							<text class="kpi-label">{{ kpi.label }}</text>
						</view>
					</view>
					<image src="@/static/images/indexBg2.png"></image>
				</view>
			</view>

			<!-- 本月各产品产量柱状图 -->
			<view class="chart-section">
				<view class="section-title">本月各产品产量</view>
				<view class="chart-card">
					<view class="chart-container">
						<productNumVue :yearMonth = "yearMonth"></productNumVue>
					</view>
				</view>
			</view>

			<!-- 本月各产品产量分布饼图 -->
			<view class="chart-section">
				<view class="section-title">本月各产品产量分布</view>
				<view class="chart-card">
					<view class="chart-container">
						<distributionVue :yearMonth = "yearMonth"></distributionVue>
					</view>
				</view>
			</view>
		</view>
		<u-datetime-picker @cancel="show = false" @confirm="onConfirm" :show="show" v-model="time" mode="year-month"></u-datetime-picker>
	</view>
</template>

<script>
	import {productionthisYear,productionthisQuarter,productionthisMonth,productionyesterday} from '@/api/product.js'
	import productNumVue from './cnps/productNum.vue'
	import distributionVue from './cnps/distribution.vue'
	import {formatTimestamp,formatTimestampBack} from '@/utils/common.js'
	export default {
		components:{
			productNumVue,
			distributionVue
		},
		data() {
			return {
				kpiData: [{
						label: '本年生产',
						value: '311.21'
					},
					{
						label: '本季生产',
						value: '311.21'
					},
					{
						label: '本月生产',
						value: '311.21'
					},
					{
						label: '昨日生产',
						value: '311.21'
					}
				],
				show: false,
				time: Number(new Date()),
				showTime:''
			}
		},
		onShow() {
			this.showTime = formatTimestamp(this.time)
			this.initData()
		},
		computed:{
			yearMonth(){
				return formatTimestampBack(this.time)
			}
		},
		methods: {
			initData(){
				productionthisYear(formatTimestampBack(this.time)).then(res=>{
					this.kpiData[0].value = res.data
				})
				productionthisQuarter(formatTimestampBack(this.time)).then(res=>{
					this.kpiData[1].value = res.data
				})
				productionthisMonth(formatTimestampBack(this.time)).then(res=>{
					this.kpiData[2].value = res.data
				})
				productionyesterday(formatTimestampBack(this.time)).then(res=>{
					this.kpiData[3].value = res.data
				})
			},
			onConfirm(value){
				this.time = value.value
				this.showTime = formatTimestamp(value.value)
				this.initData()
				this.show = false
				
			},
			goBack() {
				uni.navigateBack()
			},
			selectDate() {
				this.show = true;
			},
			getCurrentDate() {
			  const now = new Date();
			  const year = now.getFullYear();
			  const month = (now.getMonth() + 1).toString().padStart(2, '0');
			  const day = now.getDate().toString().padStart(2, '0');
			  
			  return `${year}/${month}/${day}`;
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import url('@/static/font/navBar.less');
	.production-analysis-page {
		background: #edf0f8;
		min-height: 100vh;
		position: relative;
		overflow-y: auto;
		.head-bg {
			width: 100%;
			position: absolute;
			left: 0;
			top: 0;
		}
	}

	.header{
		display: flex;
		justify-content: space-between;
		padding: 0 24rpx;
		box-sizing: border-box;
		.header-left {
			>text:nth-child(1) {
				font-weight: 600;
				font-size: 36rpx;
				color: #333;
			}
		
			>text:nth-child(2) {
				color: #313D4B;
				font-size: 26rpx;
				margin-left: 5rpx;
			}
		}
		
		.header-right {
			background-color:#C4D4FA;
			display: flex;
			justify-content: space-around;
			flex-direction: column;
			align-items: flex-end;
			padding: 0 20rpx;
			height: 60rpx;
			border-radius: 40rpx;
		}
		.update-time {
			font-size: 20rpx;
			color: #666;
		}
		
		.date-selector {
			display: flex;
			align-items: center;
			gap: 8rpx;
		
			.date-text {
				font-size: 24rpx;
				color: #333;
			}
		}
	}

	.page-content {
		width: 100%;
		box-sizing: border-box;
		// padding: 20rpx 30rpx;
		position: absolute;
	}

	.kpi-section {
		margin-bottom: 30rpx;
		padding: 20rpx 24rpx 0 24rpx;
	}

	.kpi-cards {
		position: relative;
		height: 164rpx;
		>view:first-child{
			display: flex;
			border-radius: 20rpx;
			display: flex;
			height: 164rpx;
			justify-content: space-around;
			padding: 40rpx 5rpx;
			position: absolute;
			z-index: 1;
		}
		>image{
			width: 100%;
			position: absolute;
			left: 0;
			height: 100%;
			top: 0;
		}
	}

	.kpi-card {
		// padding: 0 20rpx;
		text-align: center;
		display: flex;
		align-content: space-around;
		justify-content: center;
		flex-wrap: wrap;
	}

	.kpi-value {
		display: block;
		font-size: 26rpx;
		font-weight: 600;
		color: rgba(0, 129, 255, 1);
		margin-bottom: 8rpx;
		>text:nth-child(1){
			color:rgba(0, 129, 255, 1) ;
			font-size: 34rpx;
		}
		>text:nth-child(2){
			font-size: 18rpx;
		}
		
	}

	.kpi-label {
		display: block;
		font-size: 24rpx;
		color:rgba(0, 0, 0, 0.8);
	}

	.chart-section {
		padding: 0 24rpx;
		margin-bottom: 30rpx;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
		margin-bottom: 20rpx;
	}

	.chart-card {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx 20rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
		display:flex;
		align-items: center;
		justify-content: center;
	}

	.chart-container {
		width: 100%;
		// height: 400rpx;
		// margin-bottom: 30rpx;
	}
</style>